<?php

namespace App\Http\Controllers\CRM;

use App\Http\Controllers\Controller;
use App\Http\Requests\CRM\MemberLevelRequest;
use App\Http\Resources\CRM\MemberLevelResource;
use App\Members\MemberLevel;
use App\Models\UserLevel;
use Illuminate\Http\Request;

class MemberLevelController extends Controller
{
    public function getLevels()
    {
        $level = UserLevel::where("type", "member")->first();
        if (empty($level)) {
            $level = new UserLevel();
            $level->type = "member";
            $level->content = new MemberLevel();
            $level->save();
        }

        return new MemberLevelResource($level);
    }

    public function setLevels(MemberLevelRequest $request)
    {
        $level = UserLevel::where("type", "member")->first();

        $content = $level->content;
        $content->monthLevelScope = $request->monthLevelScope;
        $content->allLevelScope = $request->allLevelScope;

        $level->content = $content;
        $level->save();

        return new MemberLevelResource($level);
    }
}
